#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMap>
#include <QUrl>
#include <QString>
#include <QNetworkReply>

MainWindow::MainWindow(QWidget *parent, QString *url)
    : QWidget(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

	// создаем капсболд
	boldfont.setFamily("Droid Sans");
	boldfont.setPixelSize(12);
	boldfont.setWeight(600);
	boldfont.setBold(true);

	// расположение окна по центру экрана
	desktop = QApplication::desktop();
	int appWidth = width();
	int appHeight = height();
	int appWidthPos = desktop->width()/2 - appWidth/2;
	int appHeightPos = desktop->height()/2 - appHeight/2;
	move(appWidthPos, appHeightPos);

	// меняем верхнюю надпись и заголовок окна
	ui->label->setFont(boldfont);
	ui->label->setAlignment(Qt::AlignCenter);
	ui->label->setText(qApp->applicationName() + tr(" V") + qApp->applicationVersion());
	this->setWindowTitle(qApp->applicationName() + tr(" V") + qApp->applicationVersion());

	keyList << "linux" << "windows" << "macos";
	spintimer = new QTimer(this);
	spintimer->setInterval(200);

    connect(ui->pushButtonRun, SIGNAL(clicked()), this, SLOT(run()));
    connect(ui->pushButtonClose, SIGNAL(clicked()), this, SLOT(close()));
    connect(&parseThread, SIGNAL(finished()), this, SLOT(setResults()));
	connect(ui->sButton, SIGNAL(toggled(bool)), this, SLOT(boardSwitcher()));
	connect(ui->muButton, SIGNAL(toggled(bool)), this, SLOT(boardSwitcher()));
	connect(spintimer, SIGNAL(timeout()), this, SLOT(spinnerDraw()));

    if (url != 0)
    {
        ui->lineEdit->setText(*url);
        run();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::run()
{
    ui->pushButtonRun->setEnabled(false);
	spintimer->start();
	spinstring.append("8::o");

	QRegExp rxBord("/(\\w+)/");

	if (rxBord.indexIn(ui->lineEdit->text()) != -1)
	{
		if (rxBord.cap(1) == "s")
			ui->sButton->setChecked(true);
		else if (rxBord.cap(1) == "mu")
			ui->muButton->setChecked(true);
	}

	this->boardSwitcher();
    parseThread.begin(QUrl(ui->lineEdit->text()));
}

void MainWindow::setResults()
{
    if (parseThread.getReply()->error() != QNetworkReply::NoError)
    {
        QMessageBox::critical(this, tr("Ошибка"),
                              parseThread.getReply()->errorString());
        ui->pushButtonRun->setEnabled(true);
		spintimer->stop();
		spinstring.clear();
		ui->spinLabel->clear();
        return;
    }

    QMap <QString, int> count(parseThread.getCount());

	int sumOs = count.value(keyList.at(0)) + count.value(keyList.at(1)) + count.value(keyList.at(2));

    if (sumOs)
    {
		int linuxpercent = 100 * count.value(keyList.at(0)) / sumOs;
		int windowspercent = 100 * count.value(keyList.at(1)) / sumOs;
		int macospercent = 100 * count.value(keyList.at(2)) / sumOs;
		ui->progressBarLinux->setValue(linuxpercent);
		ui->progressBarLinux->setToolTip(tr("Отсосов: ") + QString::number(count.value(keyList.at(0))) +
                                            tr(" из ") + QString::number(sumOs));

		ui->progressBarWindows->setValue(windowspercent);
		ui->progressBarWindows->setToolTip(tr("Отсосов: ") + QString::number(count.value(keyList.at(1))) +
                                            tr(" из ") + QString::number(sumOs));

		ui->progressBarMacos->setValue(macospercent);
		ui->progressBarMacos->setToolTip(tr("Отсосов: ") + QString::number(count.value(keyList.at(2))) +
                                            tr(" из ") + QString::number(sumOs));

		if (linuxpercent > windowspercent && linuxpercent > macospercent)
		{
			ui->label_2->setText(ui->label_2->text().toUpper());
			ui->label_2->setFont(boldfont);
		}
		else if (windowspercent > linuxpercent && windowspercent > macospercent)
		{
			ui->label_3->setText(ui->label_3->text().toUpper());
			ui->label_3->setFont(boldfont);
		}
		else if (macospercent > linuxpercent && macospercent > windowspercent)
		{
			ui->label_4->setText(ui->label_4->text().toUpper());
			ui->label_4->setFont(boldfont);
		}
    }

// Браузероблядство
	QRegExp rxOchan("0chan");

	if (rxOchan.indexIn(ui->lineEdit->text()) != -1)
		count.insert("firefox", count.value("firefox") - 3);

	int sumBr = count.value("firefox") + count.value("opera") + count.value("chrome");
    if (sumBr)
    {
        ui->progressBarFirefox->setValue(100 * count.value("firefox") / sumBr);
        ui->progressBarFirefox->setToolTip(tr("Отсосов: ") + QString::number(count.value("firefox")) +
                                            tr(" из ") + QString::number(sumBr));
        ui->progressBarOpera->setValue(100 * count.value("opera") / sumBr);
        ui->progressBarOpera->setToolTip(tr("Отсосов: ") + QString::number(count.value("opera")) +
                                            tr(" из ") + QString::number(sumBr));
        ui->progressBarChrome->setValue(100 * count.value("chrome") / sumBr);
        ui->progressBarChrome->setToolTip(tr("Отсосов: ") + QString::number(count.value("chrome")) +
                                            tr(" из ") + QString::number(sumBr));
    }
    ui->labelRooster->setText(QString::number(count.value("rooster")));

    ui->pushButtonRun->setEnabled(true);
	spintimer->stop();
	spinstring.clear();
	ui->spinLabel->clear();
}

void MainWindow::reset()
{
	ui->label_2->setFont(normfont);
	ui->label_3->setFont(normfont);
	ui->label_4->setFont(normfont);

    ui->progressBarLinux->setValue(0);
    ui->progressBarLinux->setToolTip(tr("Отсосов: 0 из 0"));
    ui->progressBarWindows->setValue(0);
    ui->progressBarWindows->setToolTip(tr("Отсосов: 0 из 0"));
    ui->progressBarMacos->setValue(0);
    ui->progressBarMacos->setToolTip(tr("Отсосов: 0 из 0"));

    ui->progressBarFirefox->setValue(0);
    ui->progressBarFirefox->setToolTip(tr("Отсосов: 0 из 0"));
    ui->progressBarOpera->setValue(0);
    ui->progressBarOpera->setToolTip(tr("Отсосов: 0 из 0"));
    ui->progressBarChrome->setValue(0);
    ui->progressBarChrome->setToolTip(tr("Отсосов: 0 из 0"));

    ui->labelRooster->setText("0");
}

void MainWindow::boardSwitcher()
{
	this->reset();

	QRegExp rxBord("/(\\w+)/$");

	rxBord.indexIn(ui->lineEdit->text());

	if (ui->sButton->isChecked())
	{
		if (rxBord.cap(1) == "mu")
			ui->lineEdit->setText("http://www.2-ch.ru/s/");
		ui->groupBox->setTitle(tr("ОС-бляди:"));
		ui->label_2->setText(tr("Прыщебляди"));
		ui->label_3->setText(tr("Спермобляди"));
		ui->label_4->setText(tr("Говноеды"));

		keyList.clear();
		keyList << "linux" << "windows" << "macos";

	}
	else if (ui->muButton->isChecked())
	{
		if (rxBord.cap(1) == "s")
			ui->lineEdit->setText("http://www.2-ch.ru/mu/");
		ui->groupBox->setTitle(tr("Музобляди:"));
		ui->label_2->setText(tr("Гривобесы"));
		ui->label_3->setText(tr("Объебосы"));
		ui->label_4->setText(tr("Хипсторы"));

		keyList.clear();
		keyList << "grivo" << "obebo" << "hips";
	}
}

void MainWindow::spinnerDraw()
{
	spinstring.chop(1);
	spinstring.append(":o");
	ui->spinLabel->setText(spinstring);
	if (spinstring.size() > 35)
		spinstring = QString("8::o");
}
